package ph.greenova.persistence.impl;

import ph.greenova.persistence.dao.ProjectDAO;
import ph.greenova.persistence.model.Person;
import ph.greenova.persistence.model.Project;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

/**
 * Created By: adrian
 * Date Created: 4:51 PM 6/5/13
 */
//@Repository
public class ProjectDAOImpl /*extends AbstractDAOImpl<Project>*/ implements ProjectDAO {
    private static final TreeMap<Long, Project> projects = new TreeMap<Long, Project>();
    private static final TreeMap<Long, List<Person>> assignments = new TreeMap<Long, List<Person>>();

    public ProjectDAOImpl() {
        if (projects.isEmpty() && assignments.isEmpty()) {
            Project p = new Project();
            p.setId(1l);
            p.setBudget("200 MD");
            p.setCode("1");
            p.setName("Smartforms");
            p.setRemarks("remarks 1");
            projects.put(p.getId(), p);
            assignments.put(p.getId(), new ArrayList<Person>());

            p = new Project();
            p.setId(2l);
            p.setBudget("150 MD");
            p.setCode("2");
            p.setName("Loyalty Program");
            p.setRemarks("remarks 2");
            projects.put(p.getId(), p);
            assignments.put(p.getId(), new ArrayList<Person>());

            p = new Project();
            p.setId(3l);
            p.setBudget("400 MD");
            p.setCode("3");
            p.setName("Business Mall");
            p.setRemarks("remarks 3");
            projects.put(p.getId(), p);
            assignments.put(p.getId(), new ArrayList<Person>());
        }
    }

    @Override
    public Project create(final Project entity) {

        entity.setId(Long.valueOf(projects.size() + 1));
        projects.put(entity.getId(), entity);
        assignments.put(entity.getId(), new ArrayList<Person>());
        return entity;
    }

    @Override
    public Project find(Object id) {
        return projects.get(id);
    }


    @Override
    public List<Project> findAll() {
        return new ArrayList<Project>(projects.values());
    }


    @Override
    public List<Person> getAllProjectMembers(Long projectId) {
        return assignments.get(projectId);
    }

    @Override
    public List<Person> getAllNotMembers(Long projectId) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void assignPersonToProject(final Long projectId, final Person person) {

        for (Person p : assignments.get(projectId)) {
            if (p.getId().equals(person.getId())) {
                return;
            }
        }


        assignments.get(projectId).add(person);
    }

    @Override
    public void unAssignPersonFromProject(final Long projectId, final Long personId) {
        int index = -1;
        for (Person p : assignments.get(projectId)) {
            if (p.getId().equals(personId)) {
                index = assignments.get(projectId).indexOf(p);
            }
        }

        if(index != -1){

            assignments.get(projectId).remove(index);
        }

    }


}
